<?php
/* SVN FILE: $Id: lil_media_plugin_controller.php 162 2009-10-14 19:48:36Z miha@nahtigal.com $ */
/**
 * Short description for lil_media_plugin_controller.php
 *
 * Long description for lil_media_plugin_controller.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_media_plugin
 * @subpackage    lil_media_plugin
 * @since         v 2.1
 * @version       $Revision: 162 $
 * @modifiedby    $LastChangedBy: miha@nahtigal.com $
 * @lastmodified  $Date: 2009-10-14 21:48:36 +0200 (sre, 14 okt 2009) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'Lil.LilPlugin');
/**
 * LilMediaPluginController class
 *
 * @uses          LilMediaPluginController
 * @package       lil_media_plugin
 * @subpackage    lil_media_plugin
 */
class LilMediaPluginController extends LilPluginController {
	var $name = 'LilMediaPlugin';
/**
 * handlers property
 *
 * @var array
 * @access public
 */
  var $handlers = array(
    'admin_sidebar' => array('function' => '_setAdminSidebar', 'params' => array())
  );
/**
 * _setAdminSidebar method
 *
 * Add admin sidebar elements.
 *
 * @param mixed $model
 * @access public
 * @return void
 */
	function _setAdminSidebar($controller, $sidebar) {
		$sidebar['media']['visible'] = $this->currentArea->get('id');
		$sidebar['media']['items']['tree'] = array(
			'visible' => true,
			'title' => __d('lil_media', 'Media Explorer', true),
			'url' => 
				array(
					'plugin'     => 'lil_media',
					'prefix'     => 'admin',
					'admin'		 => true,
					'controller' => 'media',
					'action'     => 'index',
				),
			'params' => array(),
			'active' =>
				$this->params['plugin'] == 'lil_media' &&
				$this->params['action'] == 'index',
			'expand' => $this->params['plugin'] == 'lil_media',
			'submenu' => array()
		);
		$sidebar['media']['items']['upload'] = array(
			'visible' => true,
			'title' => __d('lil_media', 'Manage Files', true),
			'url' => 
				array(
					'plugin'     => 'lil_media',
					'prefix'     => 'admin',
					'admin'		 => true,
					'controller' => 'media',
					'action'     => 'add',
					'path'       => 
						(isset($this->params['named']['path'])) ?
						$this->params['named']['path'] :
						null
				),
			'params' => array(),
			'active' => 
				$this->params['plugin'] == 'lil_media' && 
				$this->params['admin'] == true &&
				$this->params['controller'] == 'media' &&
				in_array($this->params['action'], array('add', 'view')),
			'expand' => 
				$this->params['plugin'] == 'lil_media' && 
				$this->params['admin'] == true &&
				$this->params['controller'] == 'media' &&
				in_array($this->params['action'], array('add', 'view', 'index')),
			'submenu' => array(
				'upload' => array(
					'visible' =>
						$this->params['plugin'] == 'lil_media' && 
						$this->params['admin'] == true &&
						$this->params['controller'] == 'media',
					'title' => __d('lil_media', 'Upload', true),
					'url'   => array(
						'plugin'     => 'lil_media',
						'prefix'     => 'admin',
						'admin'		 => true,
						'controller' => 'media',
						'action'     => 'add',
						'path'       => (isset($this->params['named']['path'])) ?
										$this->params['named']['path'] :
										null
					),
					'params' => array(),
					'active' =>
						$this->params['plugin'] == 'lil_media' &&
						$this->params['admin'] == true &&
						$this->params['controller'] == 'media' &&
						$this->params['action'] == 'add'
				),
				'delete' => array(
					'visible' =>
						$this->params['plugin'] == 'lil_media' && 
						$this->params['admin'] == true &&
						$this->params['controller'] == 'media' &&
						$this->params['action'] == 'view',
					'title' => __d('lil_media', 'Delete', true),
					'url'   => array(
						'plugin'     => 'lil_media',
						'prefix'     => 'admin',
						'admin'		 => true,
						'controller' => 'media',
						'action'     => 'delete',
						'path'       => @$this->params['named']['path'],
						'filename'   => @$this->params['named']['filename'],
					),
					'params' => array(
						'confirm' => __d('lil_media', 'Are you sure you want to permanently delete this file?', true),
					),
					'active' => false
				)
			)
		);
		$sidebar['media']['items']['create'] = array(
			'visible' => true,
			'title' => __d('lil_media', 'Manage Folders', true),
			'url' => 
				array(
					'plugin'     => 'lil_media',
					'prefix'     => 'admin',
					'admin'		 => true,
					'controller' => 'media',
					'action'     => 'mk_dir',
					'path'       => (isset($this->params['named']['path'])) ?
									$this->params['named']['path'] :
									null
				),
			'params' => array(),
			'active' => $this->params['plugin'] == 'lil_media' && $this->params['action'] == 'mk_dir',
			'expand' => 
				$this->params['plugin'] == 'lil_media' &&
				$this->params['admin'] == true &&
				$this->params['controller'] == 'media' &&
				in_array($this->params['action'], array('index', 'mk_dir')),
			'submenu' => array(
				'create' => array(
					'visible' => true,
					'title' => __d('lil_media', 'Create', true),
					'url'   => array(
						'plugin'     => 'lil_media',
						'prefix'     => 'admin',
						'admin'		 => true,
						'controller' => 'media',
						'action'     => 'mk_dir',
						'path'       => (isset($this->params['named']['path'])) ?
										$this->params['named']['path'] :
										null
					),
					'params' => array(),
					'active' =>
						$this->params['plugin'] == 'lil_media' &&
						$this->params['admin'] == true &&
						$this->params['controller'] == 'media' &&
						in_array($this->params['action'], array('mk_dir'))
				),
				'delete' => array(
					'visible' => !empty($this->params['named']['path']),
					'title' => __d('lil_media', 'Delete', true),
					'url'   => array(
						'plugin'     => 'lil_media',
						'prefix'     => 'admin',
						'admin'		 => true,
						'controller' => 'media',
						'action'     => 'rm_dir',
						'path'       => (isset($this->params['named']['path'])) ?
										$this->params['named']['path'] :
										null
					),
					'params' => array(
						'confirm' => __d('lil_media', 'Are you sure you want to delete selected folder and all its contents?', true),
					),
					'active' =>
						$this->params['plugin'] == 'lil_media' &&
						$this->params['admin'] == true &&
						$this->params['controller'] == 'media' &&
						in_array($this->params['action'], array('rm_dir'))
				)
			)
		);
		
		// fetch all folders
		$area = $this->currentArea->get();
		if (is_dir(Configure::read('LilMedia.baseDir') . $area['Area']['slug'])) {
			$this->_recurseFolders(
				Configure::read('LilMedia.baseDir') . $area['Area']['slug'],
				0,
				'',
				$sidebar['media']['items']['tree']['submenu']
			);
		}
		
		return $sidebar;
	}
	
	function _recurseFolders($base_path, $depth, $path, &$dest) {
		foreach (new DirectoryIterator($base_path) as $file_info) {
			if ($file_info->isDot()) continue;
			if (
				$file_info->isDir() &&
				substr($file_info->getFileName(), 0, 1) !== '.' &&
				substr($file_info->getFileName(), 0, 1) !== '_'
			) {
				// add to sidebar
				$dest[uniqid()] = array(
					'visible' => true,
					'title' => $file_info->getFileName(),
					'url'   => array(
						'plugin'     => 'lil_media',
						'prefix'     => 'admin',
						'admin'		 => true,
						'controller' => 'media',
						'action'     => 'index',
						'path'       => base64_encode($path . DS . $file_info->getFileName())
					),
					'params' => array('style' => 'margin-left: ' . $depth * 10 . 'px;'),
					'active' =>
						$this->params['plugin'] == 'lil_media' &&
						$this->params['controller'] == 'media' &&
						isset($this->params['named']['path']) &&
						base64_decode($this->params['named']['path']) == $path . DS . $file_info->getFileName()
				);
				$this->_recurseFolders(
					$base_path . DS . $file_info->getFileName(),
					$depth + 1,
					$path . DS . $file_info->getFileName(),
					$dest
				);
			}
		}
	}
}
?>